package com.chesnutconsulting.reports;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;

public class ReportsInitializer implements InitializingBean,
		ServletContextAware {

	private String reportsMenuName = "";
	private String reportsConfigFileExtension = "";
	private static String reportsConfigLocation = "";
	private static ServletContext servletContext;

	private static Map<String, List<UserInterfaceElement>> reportWidgetTypeMap;
	private static ReportCategory rootReportCategory;

	public String getReportsConfigLocation() {
		return reportsConfigLocation;
	}

	public void setReportsConfigLocation(String reportsConfigLocation) {
		ReportsInitializer.reportsConfigLocation = reportsConfigLocation;
	}

	public static void init() throws IOException {

		ReportsInitializer.reportWidgetTypeMap = new HashMap<String, List<UserInterfaceElement>>();
		ReportsInitializer.rootReportCategory = new ReportCategory("Root",
				"root");

		File reportConfigLocation = new File(
				servletContext.getRealPath(reportsConfigLocation));

		FileTraversal.traverse(reportConfigLocation, rootReportCategory);

		rootReportCategory.printTree("");

		System.out.println(rootReportCategory.getReportLeaves().toString());

	}

	@Override
	public void afterPropertiesSet() throws Exception {
		init();
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		ReportsInitializer.servletContext = servletContext;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public String getReportsConfigFileExtension() {
		return reportsConfigFileExtension;
	}

	public void setReportsConfigFileExtension(String reportsConfigFileExtension) {
		this.reportsConfigFileExtension = reportsConfigFileExtension;
	}

	public static Map<String, List<UserInterfaceElement>> getReportWidgetTypeMap() {
		return reportWidgetTypeMap;
	}

	public static void setReportWidgetTypeMap(
			Map<String, List<UserInterfaceElement>> reportWidgetTypeMap) {
		ReportsInitializer.reportWidgetTypeMap = reportWidgetTypeMap;
	}

	public static void main(String[] args) {
		try {
			FileTraversal
					.traverse(
							new File(
									"/Users/ichesnut/Workspaces/Dev/SQLReports/src/main/webapp/WEB-INF/reports"),
							new ReportCategory("root", ""));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static ReportCategory getRootReportCategory() {
		return rootReportCategory;
	}

	public static void setRootReportCategory(ReportCategory rootReportCategory) {
		ReportsInitializer.rootReportCategory = rootReportCategory;
	}

	public String getReportsMenuName() {
		return reportsMenuName;
	}

	public void setReportsMenuName(String reportsMenuName) {
		this.reportsMenuName = reportsMenuName;
	}

}
